$(function(){
	var currentItem;
	var mark = "<tr class=\"jks-table-body\">" +
			"<td><input type=\"checkbox\"/></td>"+
			"<td>{{= index}}</td>" +
			"<td>{{= name}}</td>" +
			"<td>{{= desp}}</td>" +
			"<td><a>编辑</a></td>"+
			"</tr>";
	
	function refresh(){
		$("#role tr[class*=jks-table-body]").remove();
		$.ajax({
			url: "group/all",
			dataType: "json",
			complete: function(XHR, TS){
				if(TS == "success"){
					var ar = XHR.responseJSON;
					if(ar.success){
						for(var i=0; i<ar.data.length; i++){
							ar.data[i].index=i+1;
						}
						$.tmpl(mark, ar.data).appendTo("#role");
					}
				}
			}
		});
	}
	refresh();
	
	var random = new Date().getTime();
	var addDialogId = "add_" + random;
	var modifyDialogId = "modify_" +random;
	var _form_id = "id_" + random;
	var _form_name_id = 'name_' + random;
	var _form_desp_id = "desp_" + random;
	var _form_name_m_id = 'name_m_' +random;
	var _form_desp_m_id = 'desp_m_' + random;
	var dialogOpts = {
			autoOpen : false,
			width : 350,
			modal : true,
			buttons : {
				"确定" : function() {
					var param={
						name: $("#"+_form_name_id).val(),
						desp: $("#"+_form_desp_id).val()
					}
					$.ajax({
						url : 'group/add',
						type : 'POST',
						data : param,
						dataType : 'json',
						complete : function(XHR, TS) {
							$("#"+_form_name_id).val('');
							$("#"+_form_desp_id).val('');
							if (TS == "success") {
								var ar=XHR.responseJSON;
								if(ar.success){
									refresh();
								}
								else{
									alert('失败！');
								}
							}
						}
					});
					$("#"+addDialogId).dialog("close");
				},
				"取消" : function() {
					$("#"+addDialogId).dialog("close");
				}
			}
	}
	
	var dialogOptsModify = {
			autoOpen : false,
			width : 350,
			modal : true,
			buttons : {
				"确定" : function() {
					
					var param={
						id: $("#"+_form_id).val(),
						name: $("#"+_form_name_m_id).val(),
						desp: $("#"+_form_desp_m_id).val()
					}
					$.ajax({
						url : 'group/update',
						type : 'POST',
						data : param,
						dataType : 'json',
						complete : function(XHR, TS) {
							$("#"+_form_id).val('');
							$("#"+_form_name_m_id).val('');
							$("#"+_form_desp_m_id).val('');
							if (TS == "success") {
								var ar=XHR.responseJSON;
								if(ar.success){
									if(currentItem){
										currentItem.data.name = ar.data.name;
										currentItem.data.desp = ar.data.desp;
										currentItem.update();
									}
								}
								else{
									alert('失败！');
								}
							}
						}
					});
					$("#"+modifyDialogId).dialog("close");
				},
				"取消" : function() {
					$("#"+modifyDialogId).dialog("close");
				}
			}
	}
	var addMark = '<div id="'+addDialogId+'" title="添加角色">'+
	'<table style="width:100%;">'+
		'<tr>'+
		'<td style="width:60px;">名称:</td>'+
		'<td><input id="'+_form_name_id+'" type="text"/></td>'+
		'</tr>'+
		
		'<tr>'+
		'<td>描述:</td>'+
		'<td><textarea id="'+_form_desp_id+'"/></td>'+
		'</tr>'+
	'</table>'
	'</div>';
	
	var modifyMark = '<div id="'+modifyDialogId+'" title="修改角色">'+
	'<table style="width:100%;">'+
		'<tr>'+
		'<td style="width:60px;">名称:<input type="hidden" id="'+_form_id+'"/></td>'+
		'<td><input id="'+_form_name_m_id+'" type="text"/></td>'+
		'</tr>'+
		
		'<tr>'+
		'<td>描述:</td>'+
		'<td><textarea id="'+_form_desp_m_id+'"/></td>'+
		'</tr>'+
	'</table>'
	'</div>';
	$(addMark).appendTo(document.body).dialog(dialogOpts);
	$(modifyMark).appendTo(document.body).dialog(dialogOptsModify);
	$("#add").click(function(){
		$("#" + addDialogId).dialog("open");
	});
	$("#refresh").click(function(){
		refresh();
	});
	$("#role").delegate("tr > td:last-child", "click", function(){
		var item = $.tmplItem(this);
		$("#"+_form_id).val(item.data.id);
		$("#"+_form_name_m_id).val(item.data.name);
		$("#"+_form_desp_m_id).val(item.data.desp);
		$("#"+modifyDialogId).dialog("open");
		currentItem = item;
	});
});